package com.wchen.nio.capter2_2;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @作者： wchen
 * @描述：
 * @创建时间： 2018/7/30 20:29
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
public class TimeServer {


    public static void main(String[] args) {
        int port = 2005;

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("The time server start in port " + port);
            Socket socket = null;
            TimeServerHandlerExecutePool timeServerHandlerExecutePool = new TimeServerHandlerExecutePool(50, 1000);
            while (true){
                socket = serverSocket.accept();
                System.out.println("The server is accept a client!");
                timeServerHandlerExecutePool.execute(new TimeServerHandle(socket));
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(serverSocket != null){
                System.out.println("Time server closed");
                try {
                    serverSocket.close();
                }catch (IOException e1){
                    e1.printStackTrace();
                }
                serverSocket = null;
            }
        }

    }



}
